package com.jiantong.web.open.app.work;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.model.ButcherAdmissionInspection;
import com.jiantong.work.service.ButcherAdmissionInspectionReadService;
import com.jiantong.work.service.ButcherAdmissionInspectionWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/17.
 * 入场监督检查
 */
@Slf4j
@OpenBean
public class ButcherAdmissionInspectionApis {

    @RpcConsumer
    private ButcherAdmissionInspectionReadService butcherAdmissionInspectionReadService;

    @RpcConsumer
    private ButcherAdmissionInspectionWriteService butcherAdmissionInspectionWriteService;

    @OpenMethod(
            key = "find.butcher.admission.inspection.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public ButcherAdmissionInspection findById(Long id){
        LoginUser loginUser = UserUtil.getCurrentUser();
        Response<ButcherAdmissionInspection> butcherAdmissionInspectionResponse = butcherAdmissionInspectionReadService.findById(id);
        if(!butcherAdmissionInspectionResponse.isSuccess()){
            log.error("failed to find ButcherAdmissionInspection by id = {}, case{}",id,butcherAdmissionInspectionResponse.getError());
            throw new OPServerException(butcherAdmissionInspectionResponse.getError());
        }
        return butcherAdmissionInspectionResponse.getResult();
    }

    @OpenMethod(
            key = "paging.butcher.admission.inspection",
            paramNames = {"pageNo","pageSize","butcherSelected"},
            httpMethods = RequestMethod.GET
    )
    public Paging<ButcherAdmissionInspection> pagingButcherAdmissionInspection(Integer pageNo, Integer pageSize, Integer butcherSelected){
        LoginUser loginUser = UserUtil.getCurrentUser();
        Response<Paging<ButcherAdmissionInspection>> pagingResponse = butcherAdmissionInspectionReadService.pagingButcherAdmissionInspection(pageNo , pageSize , butcherSelected,loginUser);
        if(!pagingResponse.isSuccess()){
            log.error("failed to paging butcherAdmissionInspection by id = {}, case{}",butcherSelected,pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

    @OpenMethod(
            key = "paging.butcher.admission.inspection.by.butcher.factory",
            paramNames = {"pageNo","pageSize"},
            httpMethods = RequestMethod.GET
    )
    public Paging<ButcherAdmissionInspection> pagingButcherAdmissionInspectionByButcherFactoryId(Integer pageNo, Integer pageSize){
        LoginUser loginUser = UserUtil.getCurrentUser();
        Response<Paging<ButcherAdmissionInspection>> pagingResponse = butcherAdmissionInspectionReadService.pagingButcherAdmissionInspectionByButcherFactoryId(pageNo , pageSize,loginUser);
        if(!pagingResponse.isSuccess()){
            log.error("failed to paging butcherAdmissionInspection, case{}",pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

    @OpenMethod(
            key = "create.butcher.admission.inspection",
            paramNames = {"butcherAdmissionInspection","toStatus"},
            httpMethods = RequestMethod.POST
    )
    public Map<String , Object> createButcherAdmissionInspection(ButcherAdmissionInspection butcherAdmissionInspection, Integer toStatus){
        LoginUser loginUser = UserUtil.getCurrentUser();
        Response<Map<String , Object>> response = butcherAdmissionInspectionWriteService.create(butcherAdmissionInspection ,toStatus,loginUser);
        if(!response.isSuccess()) {
            log.error("failed to createButcherAdmissionInspection error = {}, cause : {}", butcherAdmissionInspection, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "update.butcher.admission.inspection",
            paramNames = {"butcherAdmissionInspection","toStatus"},
            httpMethods = RequestMethod.PUT
    )
    public  Map<String , Object> updateButcherAdmissionInspection(ButcherAdmissionInspection butcherAdmissionInspection , Integer toStatus){
        LoginUser loginUser = UserUtil.getCurrentUser();
        if(butcherAdmissionInspection.getId() == null){
            log.error("failed to update butcherAdmissionInspection error = {}, cause : {}", butcherAdmissionInspection,butcherAdmissionInspection.getId());
            throw new OPClientException("butcherAdmissionInspection.id.is.null");
        }
        Response<Map<String,Object>> statusResponse = butcherAdmissionInspectionReadService.findStatusById(butcherAdmissionInspection.getId());
        if(statusResponse.isSuccess() && !statusResponse.getResult().get("status").equals(butcherAdmissionInspection.getStatus())){
            log.error("butcherAdmissionInspection is already submitted error = {}, cause : {}", toStatus,statusResponse.getResult().get("status"));
            throw new OPClientException("this.report.is.already.submitted");
        }
        Response< Map<String , Object>> response = butcherAdmissionInspectionWriteService.update(butcherAdmissionInspection,toStatus,loginUser);
        if(!response.isSuccess()){
            log.error("failed to update butcherAdmissionInspection error = {}, cause : {}", butcherAdmissionInspection, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "find.sum.butcher.admission.form",
            paramNames = {"fromDate","toDate","productType"},
            httpMethods = RequestMethod.GET
    )
    public List<Map<String ,BigDecimal>> findSum(String fromDate , String toDate , Integer productType){
        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }
        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String , BigDecimal>>> response = butcherAdmissionInspectionReadService.findSum(start,end,productType);
        if(!response.isSuccess()){
            log.error("find.sum.normal case : {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "find.sum.butcher.admission.form.is.local",
            paramNames = {"fromDate","toDate","productType"},
            httpMethods = RequestMethod.GET
    )
    public Map<String ,Integer> findSumIsLocal(String fromDate , String toDate , Integer productType){
        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }
        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String , Integer>>> response = butcherAdmissionInspectionReadService.findSumIsLocal(start,end,productType);
        if(!response.isSuccess()){
            log.error("find.sum.is.local case : {}",response.getError());
            throw new OPServerException(response.getError());
        }
        Map<String , Integer> result = Maps.newHashMap();
        for(Map<String ,Integer> map : response.getResult()){
            if(map.get("isLocal") == 1){
                result.put("local",map.get("qualifiedQuantity"));
            }else{
                result.put("nonLocal",map.get("qualifiedQuantity"));
            }
        }
        return result;
    }

    @OpenMethod(
            key = "find.sum.butcher.admission.form.butcher.factory",
            paramNames = {"fromDate","toDate","productType"},
            httpMethods = RequestMethod.GET
    )
    public List<Map<String ,BigDecimal>> findSumButcherFactory(String fromDate , String toDate , Integer productType){
        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }
        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String , BigDecimal>>> response = butcherAdmissionInspectionReadService.findSumButcherFactory(start,end,productType);
        if(!response.isSuccess()){
            log.error("find.sum.is.local case : {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}
